/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Classes.Cliente;
import Classes.Veiculo;
import Controle.ControleCliente;
import Controle.ControleVeiculo;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Bruno
 */
public class NovoVeiculo extends javax.swing.JDialog {

    String cod;
    /**
     * Creates new form NovoVeiculo
     */
    public NovoVeiculo(String codigo) {
        initComponents();
        setModal(true);
        msgErro.setVisible(false);
        jLabeCor.setVisible(false);
        jLabelModelo.setVisible(false);
        jLabelPlaca.setVisible(false);
        jTextCor.setVisible(false);
        jTextModelo.setVisible(false);
        jTextPlaca.setVisible(false);
        jBotaoConfirma.setVisible(false);
        cod = codigo;
        jTextCliente.setText(cod);
        
        try {
            ControleVeiculo cv = new ControleVeiculo();
            ArrayList<Veiculo> vv = new ArrayList<Veiculo>();
            vv = cv.listaVeiculosCliente(codigo);
            Object[][] tb = new Object[vv.size()][3];
            for (int i = 0; i < vv.size(); i++) {
                tb[i][0] = vv.get(i).getPlaca();
                tb[i][1] = vv.get(i).getModelo();
                tb[i][2] = vv.get(i).getCor();
            }
            tabela.setModel(new javax.swing.table.DefaultTableModel(tb,
                    new String[]{
                        "Placa", "Modelo", "Cor"
                    }) {

                Class[] types = new Class[]{
                    java.lang.String.class, java.lang.String.class, java.lang.String.class
                };
                boolean[] canEdit = new boolean[]{
                    false, false, false
                };

                public Class getColumnClass(int columnIndex) {
                    return types[columnIndex];
                }

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit[columnIndex];
                }
            });
            tabela.repaint();
        } catch (SQLException ex) {
            Logger.getLogger(CadastroCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        botaoInclui = new javax.swing.JButton();
        msgErro = new javax.swing.JLabel();
        textoTituloJanela = new javax.swing.JLabel();
        jTextPlaca = new javax.swing.JTextField();
        jLabelPlaca = new javax.swing.JLabel();
        jTextModelo = new javax.swing.JTextField();
        jLabelModelo = new javax.swing.JLabel();
        jTextCor = new javax.swing.JTextField();
        jLabeCor = new javax.swing.JLabel();
        jBotaoConfirma = new javax.swing.JButton();
        jBotaoFinaliza = new javax.swing.JButton();
        jTextCliente = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Placa", "Modelo", "Cor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.setColumnSelectionAllowed(true);
        tabela.getTableHeader().setReorderingAllowed(false);
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);

        botaoInclui.setText("Incluir Novo Veículo");
        botaoInclui.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoIncluiActionPerformed(evt);
            }
        });

        msgErro.setFont(new java.awt.Font("Tahoma", 1, 10)); // NOI18N
        msgErro.setForeground(new java.awt.Color(255, 0, 0));
        msgErro.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        msgErro.setText("Selecione algum item na tabela");

        textoTituloJanela.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        textoTituloJanela.setText("Veículos do Cliente");

        jLabelPlaca.setText("Placa:");

        jLabelModelo.setText("Modelo:");

        jLabeCor.setText("Cor:");

        jBotaoConfirma.setText("Confirma Inclusão");
        jBotaoConfirma.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBotaoConfirmaActionPerformed(evt);
            }
        });

        jBotaoFinaliza.setText("Finalizar Cadastro");
        jBotaoFinaliza.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBotaoFinalizaActionPerformed(evt);
            }
        });

        jTextCliente.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textoTituloJanela)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabelPlaca)
                            .addComponent(jLabelModelo)
                            .addComponent(jLabeCor))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextPlaca, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextModelo, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextCor, javax.swing.GroupLayout.Alignment.TRAILING)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jBotaoConfirma))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(botaoInclui, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(msgErro, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
                            .addComponent(jBotaoFinaliza))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(msgErro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(botaoInclui)
                        .addGap(60, 60, 60)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextPlaca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelPlaca))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelModelo))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabeCor)
                            .addComponent(jTextCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jBotaoConfirma)
                        .addGap(21, 21, 21)
                        .addComponent(jBotaoFinaliza))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textoTituloJanela, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jTextCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        msgErro.setVisible(false);
    }//GEN-LAST:event_tabelaMouseClicked

    private void botaoIncluiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoIncluiActionPerformed
        msgErro.setVisible(false);
        jLabeCor.setVisible(true);
        jLabelModelo.setVisible(true);
        jLabelPlaca.setVisible(true);
        jBotaoConfirma.setVisible(true);
        jTextCor.setVisible(true);
        jTextModelo.setVisible(true);
        jTextPlaca.setVisible(true);
        botaoInclui.setVisible(false);
    }//GEN-LAST:event_botaoIncluiActionPerformed

    private void jBotaoConfirmaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBotaoConfirmaActionPerformed
        if (jTextPlaca.getText().equalsIgnoreCase("") || jTextModelo.getText().equalsIgnoreCase("")) {
            JOptionPane.showMessageDialog(null, "Preencha a PLACA e o MODELO.");
        } else {
            jLabeCor.setVisible(false);
            jLabelModelo.setVisible(false);
            jLabelPlaca.setVisible(false);
            jBotaoConfirma.setVisible(false);
            jTextCor.setVisible(false);
            jTextModelo.setVisible(false);
            jTextPlaca.setVisible(false);
            jTextPlaca.setText(jTextPlaca.getText().toUpperCase());
            Object[] linha = new Object[3];
            linha[0] = jTextPlaca.getText();
            linha[1] = jTextModelo.getText();
            linha[2] = jTextCor.getText();
            DefaultTableModel model = (DefaultTableModel) tabela.getModel();
            model.addRow(linha);
            tabela.repaint();
            botaoInclui.setVisible(true);

            Veiculo v = new Veiculo(jTextPlaca.getText(), jTextModelo.getText(), jTextCor.getText(), "1");
            v.setCod_cliente(cod);

            try {
                ControleVeiculo cv = new ControleVeiculo();
                cv.adicionaVeiculo(v);
            } catch (SQLException ex) {
                Logger.getLogger(NovoVeiculo.class.getName()).log(Level.SEVERE, null, ex);
            }

            jTextCor.setText("");
            jTextModelo.setText("");
            jTextPlaca.setText("");


        }
    }//GEN-LAST:event_jBotaoConfirmaActionPerformed

    private void jBotaoFinalizaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBotaoFinalizaActionPerformed
        dispose();
    }//GEN-LAST:event_jBotaoFinalizaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoInclui;
    private javax.swing.JButton jBotaoConfirma;
    private javax.swing.JButton jBotaoFinaliza;
    private javax.swing.JLabel jLabeCor;
    private javax.swing.JLabel jLabelModelo;
    private javax.swing.JLabel jLabelPlaca;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextCliente;
    private javax.swing.JTextField jTextCor;
    private javax.swing.JTextField jTextModelo;
    private javax.swing.JTextField jTextPlaca;
    private static javax.swing.JLabel msgErro;
    private javax.swing.JTable tabela;
    private javax.swing.JLabel textoTituloJanela;
    // End of variables declaration//GEN-END:variables
    int i = 0;
}
